package com.fenghongzhang.day009view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class MyRefresh extends LinearLayout {

    private static final String TAG = "MyRefresh";
    LinearLayout linearLayout;
    LayoutParams layoutParams;

    PointF pointF = new PointF();
    public MyRefresh(Context context) {
        super(context);
        init();
    }

    public MyRefresh(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    private void init() {
         linearLayout = new LinearLayout(getContext());
         layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200);
         layoutParams.topMargin = -200;
         linearLayout.setLayoutParams(layoutParams);
         linearLayout.setBackgroundColor(Color.RED);
         addView(linearLayout);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                pointF.x = event.getX();
                pointF.y = event.getY(); //100
                break;
            case MotionEvent.ACTION_MOVE:
                layoutParams = (LayoutParams) linearLayout.getLayoutParams();
                //104 - 103;
                layoutParams.topMargin += event.getY() -pointF.y;

                if(layoutParams.topMargin > 0){
                    layoutParams.topMargin = 0;
                }
                if(layoutParams.topMargin <-200){
                    layoutParams.topMargin = -200;
                }
                linearLayout.setLayoutParams(layoutParams);
                pointF.y = event.getY();
                pointF.x = event.getX();
                break;
            case MotionEvent.ACTION_UP:
                //
                if(layoutParams.topMargin > -100){
                    //打开
                    startAnim(layoutParams.topMargin,0);
                }else{
                    //关闭
                    startAnim(layoutParams.topMargin,-200);
                }
                break;
        }

        //一定要自己消费
        return true;
    }

    private void startAnim(int topMargin, int i) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(topMargin, i);//-110 0
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer animatedValue = (Integer) animation.getAnimatedValue();
                layoutParams = (LayoutParams) linearLayout.getLayoutParams();
                layoutParams.topMargin = animatedValue;
                linearLayout.setLayoutParams(layoutParams);
            }
        });
        valueAnimator.start();
    }

}
